/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ocr.server;

import cn.ac.iie.di.ocr.handler.OCRHandler;
import cn.ac.iie.di.ocr.handler.RocketMqClient;
import cn.ac.iie.di.ocr.kill.KillHandler;
import cn.ac.iie.di.ocr.mpp.RetryTask;
import cn.ac.iie.di.ocr.mpp.SelectMppThread;
import cn.ac.iie.di.ocr.mpp.UpdateMppThreadBak;
import cn.ac.iie.di.ocr.msg.JsonMsgSender;
import cn.ac.iie.di.ocr.retry.JsonRetrySender;
import cn.ac.iie.di.ocr.util.GlobalParameters;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Administrator
 */
public class SelectMppStartUp {

    static Logger logger = null;

    static {
        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource("log4j.properties"));
        logger = Logger.getLogger(SelectMppStartUp.class.getName());
    }

    public static void main(String[] args) {
        try {

            KillHandler killhandle = new KillHandler();
            killhandle.registerSignal("TERM");
            //初始化参数
            GlobalParameters.init();
            //发送消息队列
            sendMQ();
            //处理消息
            handler(killhandle);
            getMpp();
            
            while (true) {                
                logger.info("GlobalParameters.picHandlerQueue: "+GlobalParameters.picHandlerQueue.size());
                logger.info("GlobalParameters.mppQueue: "+GlobalParameters.mppQueue.size());
                logger.info("GlobalParameters.sendRetryQueue: "+GlobalParameters.sendRetryQueue.size());
                logger.info("GlobalParameters.mppQueue: "+GlobalParameters.mppQueue.size());
                logger.info("GlobalParameters.sendMsgQueue: "+GlobalParameters.sendMsgQueue.size());
                Thread.sleep(5000);
            }

        } catch (Exception e) {
            logger.error(e, e);
        }

    }

    public static void getMpp() {
//        for (int i = 0; i < 3; i++) {
//            Thread thread = new Thread(new UpdateMppThreadBak());
//            thread.setName("SelectMppThread" + "_" + i);
//            thread.start();
//        }
        for (int i = 0; i < GlobalParameters.selectMppSize; i++) {
            Thread thread = new Thread(new SelectMppThread());
            thread.setName("SelectMppThread" + "_" + i);
            thread.start();
        }
        Thread t = new Thread(new RetryTask());
        t.setName("RetryTask");
        t.start();
    }

    public static void handler(KillHandler killhandle) {
        //接收消息
        RocketMqClient mqClient = new RocketMqClient(GlobalParameters.pic_from_topic + ":" + GlobalParameters.pic_from_group, killhandle);
        Thread recvMessageThread = new Thread(mqClient);
        recvMessageThread.setName("###ReceiveMessage");
        recvMessageThread.start();

        //处理消息
        Thread picThread = new Thread(new OCRHandler());
        picThread.setName("###GwxHandler");
        picThread.start();
    }

    public static void sendMQ() {
        Thread picSendThread = new Thread(new JsonMsgSender());
        picSendThread.setName("JsonMsgSender");
        picSendThread.start();
        Thread retrySendThread = new Thread(new JsonRetrySender());
        retrySendThread.setName("JsonRetrySender");
        retrySendThread.start();
    }

}
